package cn.stylefeng.guns.modular.order.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.order.entity.WeightPrice;
import cn.stylefeng.guns.modular.order.mapper.WeightPriceMapper;
import cn.stylefeng.guns.modular.order.model.params.WeightPriceParam;
import cn.stylefeng.guns.modular.order.model.result.WeightPriceResult;
import  cn.stylefeng.guns.modular.order.service.WeightPriceService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 重量与价格表 服务实现类
 * </p>
 *
 * @author zhou
 * @since 2019-08-05
 */
@Service
public class WeightPriceServiceImpl extends ServiceImpl<WeightPriceMapper, WeightPrice> implements WeightPriceService {

    @Autowired
    WeightPriceMapper weightPriceMapper;
    @Override
    public void add(WeightPriceParam param){
        WeightPrice entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WeightPriceParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(WeightPriceParam param){
        WeightPrice oldEntity = getOldEntity(param);
        WeightPrice newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public WeightPriceResult findBySpec(WeightPriceParam param){
        return null;
    }

    @Override
    public List<WeightPriceResult> findListBySpec(WeightPriceParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(WeightPriceParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<WeightPriceResult> getAll() {
        return weightPriceMapper.getAll();
    }

    private Serializable getKey(WeightPriceParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private WeightPrice getOldEntity(WeightPriceParam param) {
        return this.getById(getKey(param));
    }

    private WeightPrice getEntity(WeightPriceParam param) {
        WeightPrice entity = new WeightPrice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
